package net.hn.hnms.biz.safety.location.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.hn.hnms.biz.api.feign.model.DataStateVo;
import net.hn.hnms.biz.api.safety.gasext.fegin.RemoteDwsService;
import net.hn.hnms.biz.api.safety.location.model.*;
import net.hn.hnms.biz.safety.location.domain.*;
import net.hn.hnms.biz.safety.location.repository.DwdJxryNowCarInformationRepository;
import net.hn.hnms.biz.safety.location.service.LocationService;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.redis.utils.RedisUtils;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 智呈物联 pan'sx
 * 2022/7/26 16:24
 * 1.0 version
 */
@RestController
@RequestMapping("riskMonitor/location")
@Api(value = "人员定位控制器", tags = {"人员定位"})
@RequiredArgsConstructor
@Slf4j
public class RiskMonitorLocationController {

    private final LocationService locationService;
    private final DwdJxryNowCarInformationRepository dwdJxryNowCarInformationRepository;

    @Resource
    private RemoteDwsService remoteDwsService;
    @GetMapping("todayInfo")
    @ApiOperation("今日信息")
    @AutoSetMineCode
    public R<LocationTodayInfo> todayInfo(String mineCode) {
        LocationTodayInfo locationTodayInfo = locationService.todayInfo(mineCode);
        return R.ok(locationTodayInfo);
    }

    @GetMapping("lead")
    @ApiOperation("井下带班领导")
    @AutoSetMineCode
    public R<List<LeadVo>> lead(String mineCode) {
        return R.ok(locationService.leads(mineCode));
    }

    @GetMapping("trend")
    @ApiOperation("井下人数趋势")
    @AutoSetMineCode
    public R<PeopleTrendVo> peopleTrend(@ApiParam(value = "月|周") String type, String mineCode) {
        return R.ok(locationService.peopleTrend(type, mineCode));
    }

    @GetMapping("peopleInfo")
    @ApiOperation("井下人员信息")
    @AutoSetMineCode
    public R<List<PeopleVo>> peopleInfo(@ApiParam(value = "名称") String name, String mineCode) {
        return R.ok(locationService.peopleInfo(name, mineCode));
    }

    @GetMapping("keyAreas")
    @ApiOperation("重点工作区域人员")
    @AutoSetMineCode
    public R<List<KeyAreaVo>> keyAreas(String mineCode) {
        return R.ok(locationService.keyAreas(mineCode));
    }

    @GetMapping("indicator")
    @ApiOperation("重要指标")
    @AutoSetMineCode
    public R<IndicatorVo> indicator(String mineCode) {
        return R.ok(locationService.indicator(mineCode));
    }

    @GetMapping("trail")
    @ApiOperation("人员入井后轨迹")
    @AutoSetMineCode
    public R<List<TrailVo>> trail(@ApiParam(value = "人员卡号", required = true) String staffCode, @ApiParam(value = "入井时间（yyyy-MM-dd HH:mm:ss）"
            , required = true) String inTime
            , String mineCode) {

        List<TrailVo> vos = new ArrayList<>();

        Boolean istru = RedisUtils.getCacheObject(Constants.SYS_DWS_KEY);
        if (istru == null || !istru) {
            vos = locationService.trail(staffCode, inTime, mineCode);
        } else {
            List<net.hn.hnms.biz.api.safety.gasext.domain.TrailVo> Trail = remoteDwsService.trail(staffCode, inTime, mineCode).getData();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (net.hn.hnms.biz.api.safety.gasext.domain.TrailVo trail : Trail) {
                TrailVo trailVo = new TrailVo();
                trailVo.setSubstationCode(trail.getSubstationCode());
                trailVo.setSubstationName(trail.getSubstationName());
                trailVo.setCoordinate_x(new BigDecimal(trail.getCoordinate_x()));
                trailVo.setCoordinate_y(new BigDecimal(trail.getCoordinate_y()));
                trailVo.setCoordinate_y(new BigDecimal(trail.getCoordinate_z()));
                Date parse = null;
                try {
                     parse = sdf.parse(trail.getInTime());
                    trailVo.setInTime(parse);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                vos.add(trailVo);
            }
        }
        return R.ok(vos);
    }
    @GetMapping("hisTrail")
    @ApiOperation("人员历史入井后轨迹")
    @AutoSetMineCode
    public R<List<TrailVo>> hisTrail(@ApiParam(value = "人员卡号", required = true) String staffCode, @ApiParam(value = "入井时间（yyyy-MM-dd HH:mm:ss）"
            , required = true) String inTime
            , String mineCode) throws ParseException {
        return R.ok(locationService.hisTrail(staffCode, inTime, mineCode));
    }
    @GetMapping("peopleInfoByArea")
    @ApiOperation("指定区域或分站的人员信息")
    @AutoSetMineCode
    public R<List<PeopleVo>> peopleInfoByArea(@ApiParam(value = "区域code") String areaCode
            , @ApiParam(value = "分站code") String stationCode
            , String mineCode) {
        return R.ok(locationService.peopleInfoByArea(areaCode, stationCode, mineCode));
    }

    @GetMapping("alarm/pageList")
    @ApiOperation("数据查询——报警列表")
    @AutoSetMineCode
    public R<ResponsePage<AlarmVo>> alarmList(@RequestParam(defaultValue = "") String alarmType, String beginTime, String endTime,
                                              @RequestParam(defaultValue = "0") int pageNum,
                                              @RequestParam(defaultValue = "10") int pageSize,
                                              String mineCode) {
        return R.ok(locationService.getAlarmPageList(alarmType, beginTime, endTime, pageNum, pageSize, mineCode));
    }

    @GetMapping("nowPersonnelinfo/pageList")
    @ApiOperation("数据查询——人员实时列表")
    @AutoSetMineCode
    public R<ResponsePage<JxryNowPersonnelinfoEntity>> nowPersonnelinfo(@RequestParam(defaultValue = "0") int pageNum,
                                                                        @RequestParam(defaultValue = "20") int pageSize,
                                                                        String duty,
                                                                        String dept,
                                                                        @ApiParam(value = "名字，前模糊匹配") String staffName,
                                                                        @ApiParam(value = "是否领导，1 是  0否") String leader,
                                                                        String mineCode
    ) {
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);
        return R.ok(locationService.getPageDate(duty, dept, staffName, leader, pageRequest, mineCode));
    }

    @GetMapping("hisPersonnelinfo/pageList")
    @ApiOperation("数据查询——人员历史列表")
    @AutoSetMineCode
    public R<ResponsePage<JxryHisPersonnelinfoEntity>> hisPersonnelinfo(@RequestParam(defaultValue = "0") int pageNum,
                                                                        @RequestParam(defaultValue = "20") int pageSize,
                                                                        String duty,
                                                                        String dept,
                                                                        @ApiParam(value = "名字，前模糊匹配") String staffName,
                                                                        @ApiParam(value = "是否领导，1 是  0否") String leader,
                                                                        @ApiParam(value = "开始时间，yyyy-MM-yy dd:mm:ss") String beginTime,
                                                                        @ApiParam(value = "结束时间，yyyy-MM-yy dd:mm:ss") String endTime,
                                                                        String mineCode
    ) {
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);
        return R.ok(locationService.getPageDate(duty, dept, staffName, leader, beginTime, endTime, pageRequest, mineCode));
    }

    @GetMapping("getAllDeptOrDuty")
    @ApiOperation("数据查询——获取全部部门或工种")
    @AutoSetMineCode
    public R<List<String>> getAllDeptOrDuty(
            @ApiParam(value = "dept 或 duty 默认dept")
            @RequestParam(defaultValue = "dept") String type, String mineCode) {
        if (!"dept".equals(type) && !"duty".equals(type)) {
            R.fail(400, "参数错误");
        } else {
            if ("dept".equals(type)) type = "dept_name";
        }
        return R.ok(locationService.getAllDeptOrDuty(type, mineCode));
    }

    @GetMapping("carNowData")
    @ApiOperation("车辆数据-砚北")
    @AutoSetMineCode
    public R<List<TramNowPo>> carNowData(String mineCode) {
        return R.ok(locationService.carNowData(mineCode));
    }

    @GetMapping("carData")
    @ApiOperation("车辆数据-高头窑")
    @AutoSetMineCode
    public R<List<DwdJxryNowCarInformationEntity>> carData(String mineCode) {
        return R.ok(dwdJxryNowCarInformationRepository.findByMineCode(mineCode));
    }

    @GetMapping("teamLead")
    @ApiOperation("采面领导")
    @AutoSetMineCode
    public R<AreaMiningVo> teamLead(String mineCode, String areaCode) {
        return R.ok(locationService.teamLead(mineCode, areaCode));
    }
    @GetMapping("maxTime")
    @ApiOperation("数据最新时间")
    public R<List<DataStateVo>> getMaxTime(String mineCode, String companyCode){
        return R.ok(locationService.getMaxTime( mineCode,companyCode));
    }
}
